package com.flipdf2.serialize.ppml.element;

import java.net.URI;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;


public class PPMLSuppliedResource {
	public static final String NAME = "SUPPLIED_RESOURCE";
	
	private PPMLInternalData internalData;
	private PPMLExternalData externalData;
	
	private String name;
	private String resourceName;
	private URI src;
	private String format;
	private String type;
	private String subType;
	private String scope;
	private String environment;
	private String overwrite;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (externalData != null)
			root.addContent(externalData.toXML(defaultNamespace));
		
		if (internalData != null)
			root.addContent(internalData.toXML(defaultNamespace));
		
		if (name != null) 
			root.setAttribute("Name", name);
		if (resourceName != null) 
			root.setAttribute("ResourceName", resourceName);
		if (src != null) 
			root.setAttribute("Src", src.toString());
		if (format != null) 
			root.setAttribute("Format", format);
		if (type != null) 
			root.setAttribute("Type", type);
		if (subType != null) 
			root.setAttribute("SubType", subType);
		if (scope != null) 
			root.setAttribute("Scope", scope);
		if (environment != null) 
			root.setAttribute("Environment", environment);
		if (overwrite != null) 
			root.setAttribute("Overwrite", overwrite);

		return root;
	}

	public void check() throws PPMLException {
		if (name == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Name");
		}
		if (resourceName == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "ResourceName");
		}
		if (format == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Format");
		}
		if (type == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Type");
		}
		if (scope!= null && scope.equals("Global")) {
			if (environment == null) {
				throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Environment");
			}
		}
	}
	
	public String getEnvironment() {
		return environment;
	}

	public void setEnvironment(String environment) {
		this.environment = environment;
	}

	public PPMLExternalData getExternalData() {
		return externalData;
	}

	public void setExternalData(PPMLExternalData externalData) {
		this.externalData = externalData;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public PPMLInternalData getInternalData() {
		return internalData;
	}

	public void setInternalData(PPMLInternalData internalData) {
		this.internalData = internalData;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOverwrite() {
		return overwrite;
	}

	public void setOverwrite(String overwrite) {
		this.overwrite = overwrite;
	}

	public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public URI getSrc() {
		return src;
	}

	public void setSrc(URI src) {
		this.src = src;
	}

	public String getSubType() {
		return subType;
	}

	public void setSubType(String subType) {
		this.subType = subType;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
